package com.example.stock.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.stock.entity.dao.RuleDao;
import com.example.stock.entity.dao.UnitDao;
import com.example.stock.entity.eo.RuleEo;
import com.example.stock.entity.eo.UnitEo;
import com.example.stock.service.IRuleService;
import com.example.stock.service.IUnitService;
import com.example.stock.vo.PlusVo;
import com.example.stock.vo.UnitCreateForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/unit")
@Slf4j
public class UnitController {

    @Resource
    private UnitDao unitDao;
    @Resource
    private RuleDao ruleDao;
    @Resource
    private IUnitService unitService;
    @Resource
    private IRuleService ruleService;

    @PostMapping("create")
    public Map create(@RequestBody @Validated UnitCreateForm createForm) {
        unitService.create(createForm);
        return new HashMap();

    }

    @PostMapping("up-sale")
    public Map upSale(String id) {
        UnitEo byId1 = unitDao.getById(id);
        Assert.notNull(byId1, "id不存在");
        UnitEo eo = unitService.calcUp(byId1);
        if (null == eo) {
            throw new RuntimeException("已空仓");
        }
        eo.setMark(1);
        unitDao.save(eo);
        return new HashMap();
    }

    @PostMapping("down-buy")
    public Map downBuy(String id) {
        UnitEo byId1 = unitDao.getById(id);
        Assert.notNull(byId1, "id不存在");
        UnitEo eo = unitService.calcDown(byId1);
        eo.setMark(1);
        unitDao.save(eo);
        return new HashMap();
    }

    @PostMapping("delete")
    public Object delete(String id,Double trEarn) {
        if (null == trEarn) {
            throw new RuntimeException("trEarn 不能为空");
        }
        UnitEo byId = unitDao.getById(id);
        unitService.remove(byId.getTrGroup(),trEarn);
        return new HashMap<>();
    }

    @PostMapping("clearMark")
    public Object clearMark(Integer id) {
        UnitEo eo = new UnitEo();
        eo.setId(id);
        eo.setMark(0);
        unitDao.updateById(eo);
        return new HashMap<>();
    }

    @GetMapping("list")
    public List<UnitEo> list() {
        List<UnitEo> list = unitDao.list();
        Map<Integer, List<UnitEo>> collect = list.stream().collect(Collectors.groupingBy(t -> t.getTrGroup()));
        List<UnitEo> list33 = new ArrayList<>();
        collect.forEach((k, v) -> {
            UnitEo unitEo = v.stream().max(Comparator.comparing(a -> a.getId())).get();
            list33.add(unitEo);
        });
        List<UnitEo> collect2 = list33.stream().sorted((a, b) -> b.getStQty() * b.getTrPrice() - a.getStQty() * a.getTrPrice() > 0.0 ? 1 : -1).collect(Collectors.toList());
        for (UnitEo unitEo : collect2) {
            unitEo.setDnVo(unitService.getDownVo(unitEo));
            unitEo.setUpVo(unitService.getUpVo(unitEo));
            RuleEo rule = ruleService.getById(unitEo.getRuId());
            List<RuleEo.UpDnCalc> upDnCalcList = ruleService.listUpDnCalc(rule);
            rule.setUpDnCalcList(upDnCalcList);
            unitEo.setRule(rule);
        }

        return collect2;
    }


}
